﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace YNCNumberSys
{
    /// <summary>
    /// 加密、解密函数，加密的唯一出口。
    /// 加密的具体原理未深究了，借鉴原本的加密解密函数。
    /// 2023 3 XSL mod。
    /// </summary>
    public class EnOrDeEncryptHelper
    {
        /// <summary>
        /// salt 关键字。
        /// </summary>
        private static string str_salt = "&%#@?$%)@%($)#_$)*";
        /// <summary>  
        /// 加密串。  
        /// </summary>  
        /// <param name="strText">原始串。</param>  
        /// <returns>加密后的串。</returns>  
        public static string EncryptText(String strText)
        {
            return Encrypt(strText, str_salt);
        }

        /// <summary>  
        /// 解密串。  
        /// </summary>  
        /// <param name="strText">加密的串。</param>  
        /// <returns>解密后的串。</returns>  
        public static String DecryptText(String strText)
        {
            return Decrypt(strText, str_salt);
        }

        /// <summary>  
        /// 加密函数  
        /// </summary>  
        /// <param name="strText">文件。</param>  
        /// <param name="sSaltKey"></param>  
        /// <returns></returns>  
        private static String Encrypt(String strText, String sSaltKey)
        {
            Byte[] byKey = { };
            Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(sSaltKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>  
        /// 解密函数  
        /// </summary>  
        /// <param name="strText"></param>  
        /// <param name="sDecrKey"></param>  
        /// <returns></returns>  
        private static String Decrypt(String strText, String sDecrKey)
        {
            char[] stBase = strText.ToCharArray();
            for (int i = 0; i < stBase.Length; i++)
            {
                if (stBase[i] == ' ')
                {
                    stBase[i] = '+';
                }
            }
            strText = "";
            for (int i = 0; i < stBase.Length; i++)
            {
                strText += stBase[i];
            }
            Byte[] byKey = { };
            Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            Byte[] inputByteArray = new byte[strText.Length];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }  
    }
}
